﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace TY.Utilities
{
    /// <summary>
    /// 获取实体类Attribute自定义属性
    /// </summary>
    public class EnumAttribute
    {
        /// <summary>
        /// 返回枚举项的描述信息。
        /// </summary>
        /// <param name="value">要获取描述信息的枚举项。</param>
        /// <returns>枚举想的描述信息。</returns>
        public static string GetDescription(Enum value)
        {
            Type enumType = value.GetType();
            // 获取枚举常数名称。
            string name = Enum.GetName(enumType, value);
            if (name != null)
            {
                // 获取枚举字段。
                FieldInfo fieldInfo = enumType.GetField(name);
                if (fieldInfo != null)
                {
                    // 获取描述的属性。
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                        typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 获取枚举值列表
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="returnType">返回值类型</param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> GetList<T>(EnumListReturnType returnType = EnumListReturnType.Int) where T : struct
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (var value in Enum.GetValues(typeof(T)))
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                object[] objAttrs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objAttrs != null &&
                    objAttrs.Length > 0)
                {
                    DescriptionAttribute descAttr = objAttrs[0] as DescriptionAttribute;
                    Console.WriteLine(string.Format("[{0}]", descAttr.Description));
                    item.Add("ItemName", descAttr.Description);
                }
                else
                {
                    item.Add("ItemName", value.ToString());
                }
                if (returnType == EnumListReturnType.Int)
                {
                    item.Add("ItemValue", Convert.ToInt32(value));
                }
                else
                {
                    item.Add("ItemValue", value.ToString());
                }

                list.Add(item);
            }
            return list;
        }
        /// <summary>
        /// 枚举列表返回值类型
        /// </summary>
        public enum EnumListReturnType
        {
            /// <summary>
            /// 整形
            /// </summary>
            Int,
            /// <summary>
            /// 字符串
            /// </summary>
            String
        }
    }
}
